//
//  XYTHomePgaeViewController.m
//  XYTPayOrderSystem
//
//  Created by wupeng on 2017/8/23.
//  Copyright © 2017年 wupeng. All rights reserved.
//

#import "XYTHomePgaeViewController.h"
#import "XYTHomePageCell.h"
#import "JYCarousel.h"
#import "XYTButton.h"
#define mainBtn_Width 130.0f
#define mainBtn_Height 130.0f
#define BtnSpace_Hmid 25.0f
#define BtnSpace_Vmid 25.0f
#define leftSpace (kScreen_Width - mainBtn_Width*2 - BtnSpace_Hmid)/2
#define topSpace (kScreen_Height - mainBtn_Height*2 - BtnSpace_Vmid)/2

@interface XYTHomePgaeViewController ()<UITableViewDelegate,UITableViewDataSource,XYTButtonDelegate>

@property (nonatomic, strong) NSArray *btnTitles;

@property (nonatomic, strong) JYCarousel *wheelView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) UIView *headerView;

@end

@implementation XYTHomePgaeViewController

#pragma mark - lifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"首页";
    [self setupUI];
    
    [self loadData];
    
}

- (void)setupUI{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-44) style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.tableHeaderView = self.headerView;
    [self.view addSubview:tableView];
    
}

#pragma mark - privite Methods

- (void)loadData{
    
    [self.tableView reloadData];
    
    [_wheelView startCarouselWithArray:[NSMutableArray arrayWithArray:@[@"QQ20170910-110944@2x.png",@"QQ20170910-110944@2x.png",@"QQ20170910-110944@2x.png"]]];
}

- (void)setupWheelView:(UIView *)view{
    _wheelView= [[JYCarousel alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 150) configBlock:^JYConfiguration *(JYConfiguration *carouselConfig) {
        carouselConfig.pageContollType = MiddlePageControl;
        carouselConfig.interValTime = 3;
        carouselConfig.placeholder = [UIImage imageNamed:@"image_default"];
        carouselConfig.animationSubtype = kCATransitionFromRight;
        return carouselConfig;
    } clickBlock:^(NSInteger index) {
        
    }];
    [view addSubview:_wheelView];
}

- (void)setupTitleIndexView:(UIView *)view{
    UIScrollView *scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 170, kScreen_Width, 70)];
    scrollerView.backgroundColor = [UIColor whiteColor];
    CGFloat titleBtnWidth = kScreen_Width/4;
    scrollerView.contentSize = CGSizeMake(titleBtnWidth*self.btnTitles.count, 70);
    scrollerView.alwaysBounceVertical = NO;
    scrollerView.showsHorizontalScrollIndicator = NO;
    for (int i=0; i<self.btnTitles.count; i++) {
        XYTButton *button = [[XYTButton alloc] initWithFrame:CGRectMake(i*titleBtnWidth, 0, titleBtnWidth, 70) delegate:self];
        button.tag = i;
        [button setTitle:self.btnTitles[i] image:@"orderSum"];
        [scrollerView addSubview:button];
    }
    [view addSubview:scrollerView];
    
}
- (void)xytButtonOnClick:(XYTButton *)button{
    XYTBaseViewController *vc = nil;
    switch (button.tag) {
        case 0: //订单录入
        {
            vc = [[XYTOrderEnteringViewController alloc] init];
        }
            break;
        case 1: //订单列表
        {
            vc = [[XYTOrderListViewController alloc] init];
        }
            break;
        case 2: //汇总报表
        {
            vc = [[XYTConsolidatedStatementViewController alloc] init];
        }
            break;
        case 3: //个人中心
        {
            vc = [[XYTPersonCenterViewController alloc] init];
            
        }
            break;
            
        default:
            break;
    }
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;//tudo
//    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;//tudo
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *licenseCellId = @"XYTHomePageCell";
    XYTHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:licenseCellId];
    if (!cell) {
        cell = [[XYTHomePageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:licenseCellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40.0f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 20)];
    header.backgroundColor = [UIColor whiteColor];
    UIView *colerView = [[UIView alloc] initWithFrame:CGRectMake(0, 2.5, 8, 20-2.5*2)];
    colerView.backgroundColor = [UIColor orangeColor];
    [header addSubview:colerView];
    UILabel *groupLab = [[UILabel alloc] initWithFrame:CGRectMake(8+5, 0, kScreen_Width-8-5, 20)];
    groupLab.font = [UIFont systemFontOfSize:12];
    groupLab.text = @"最新活动";//tudo  没数据，暂时写死
    [header addSubview:groupLab];
    return header;
}

#pragma mark - setter/getter
- (NSArray *)btnTitles{
    if (!_btnTitles) {
        _btnTitles = @[@"订单录入",@"订单列表",@"汇总报表"];
    }
    return _btnTitles;
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 250)];
        _headerView.backgroundColor = [UIColor clearColor];
        [self setupWheelView:_headerView];
        [self setupTitleIndexView:_headerView];
    }
    return _headerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
